var util = require('../../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    hasData: true,
    iotBox: []
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onLoad: function () {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--库存管理" });
    wx.showToast({
      title: "数据加载中...",
      icon: 'loading',
      duration: 3000
    });
    this.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  loadData: function () {
    var that = this;
    wx.request({
      url: app.server + '/wechat/wxapplet/getWarehouseStock',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        wx.hideToast();
        console.log(res.data)
        var wh_data = res.data.data[0], whl_data = res.data.data[1], stock_data = res.data.data[2], warehouseData = [];
        var iotbox = that.data.iotbox;
        if (res.data.success && wh_data.length > 0) {
          that.setData({
            hasData: true
          });
          for (var i = 0; i < wh_data.length; i++) {
            warehouseData.push({
              ID: wh_data[i].ID,
              name: wh_data[i].name,
              count: wh_data[i].count||0,
              goods: []
            });
          }; console.log(warehouseData)
          for (var i = 0; i < warehouseData.length; i++) {
            var WID = warehouseData[i].ID, goods = warehouseData[i].goods;
            for (var j = 0; j < stock_data.length; j++) {
              if (WID == stock_data[j].warehouse) {
                goods.push({
                  goods_name: stock_data[j].goods_name,
                  count: stock_data[j].count,
                  goods: stock_data[j].goods
                });
              }
            };
          };
          that.setData({
            warehouseData: warehouseData
          });

        } else {
          that.setData({
            hasData: false
          })
        };
      },
      fail: function (error) {
        wx.hideToast();
        console.log(error);
        that.setData({
          hasData: false
        })
      }
    })
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    console.log(e.detail.value)
    this.setData({
      inputVal: e.detail.value
    });
  },
  calltodriver: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.target.id
    })
  },
  stockInOutForm: function (e) {
    wx.navigateTo({
      url: '/pages/warehouse/stockInOut?warehouse=' + e.currentTarget.id
    })
  },
  warehouseMonitor: function () {
    wx.redirectTo({
      url: '/pages/warehouse/warehouse'
    })
  },
  warehouseStock: function () {
    wx.redirectTo({
      url: '/pages/warehouse/stock'
    })
  },
  goodsManage: function () {
    wx.redirectTo({
      url: '/pages/warehouse/goods'
    })
  }
})
